package com.job.usercenter.controller;


import com.job.commonutils.JwtUtils;
import com.job.commonutils.R;
import com.job.usercenter.entity.Admin;
import com.job.usercenter.entity.vo.RegisterVo;
import com.job.usercenter.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author myt
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/usercenter/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService  adminService;
    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody Admin admin){

        //调用service方法实现登录
        String token = adminService.login(admin);
        //返回token值
        return R.ok().data("token",token);
    }


    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        adminService.register(registerVo);
        return R.ok();
    }
    //根据token获取用户信息
//    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        Admin user = adminService.getById(userId);
        return R.ok().data("userInfo",user);

//        try {
//            String memberId = JwtUtils.getMemberIdByJwtToken(request);
//            LoginInfoVo loginInfoVo = memberService.getLoginInfo(memberId);
//            return R.ok().data("item", loginInfoVo);
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new GuliException(20001,"error");
//        }
    }

}

